package io.wongxd.demo.page

import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.rememberScrollState
import androidx.compose.foundation.verticalScroll
import androidx.compose.material3.Text
import androidx.compose.runtime.*
import androidx.compose.ui.Modifier
import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.platform.LocalLifecycleOwner
import androidx.compose.ui.tooling.preview.Preview
import io.wongxd.solution.util.cache.ACache
import io.wongxd.solution.baseView.BaseComposeToolBarFgt
import io.wongxd.solution.compose.common.CommonButton
import io.wongxd.solution.util.cache.CachePolicy
import io.wongxd.solution.util.cache.CoroutinesCache
import io.wongxd.solution.util.tools.yyyyMMddHHmmss
import kotlinx.coroutines.delay
import kotlinx.coroutines.launch
import java.util.concurrent.TimeUnit

class FgtCache : BaseComposeToolBarFgt() {
    override fun lazyInit() {

    }

    override val title: String
        get() = "FgtCache"


    @Composable
    override fun Render() {
        val ctx = LocalContext.current

        val lifecycleOwner = LocalLifecycleOwner.current

        val scope = rememberCoroutineScope()

//        LaunchedEffect(key1 = Unit, block = {
//            ACache.init(
//                encryptStrategy = AesRsaEncrypt.getInstance(requireContext()),
//                cachePath = requireContext().cacheDir.absolutePath + "/ACache",
//                encrypt = true
//            )
//        })

        Column(
            modifier = Modifier
                .fillMaxSize()
                .verticalScroll(rememberScrollState())
        ) {

            var tempStr by remember {
                mutableStateOf("")
            }

            Text(text = tempStr)

            CommonButton(text = "保存") {
                ACache.getCache().putString("你好", "老地方拉丝机的")
            }

            CommonButton(text = "读取") {
                tempStr = ACache.getCache().getString("你好", "empty")
            }

            CommonButton(text = "删除") {
                ACache.getCache().remove("你好")
            }

            var coroutinesStr by remember {
                mutableStateOf("")
            }

            Text(text = coroutinesStr)

            val coroutinesCache = remember {
                CoroutinesCache(ctx, lifecycleOwner = lifecycleOwner)
            }

            CommonButton(text = "coroutinesCache 读取") {
                scope.launch {
                    coroutinesStr = getCoroutineCache(coroutinesCache)
                }
            }

            CommonButton(text = "coroutinesCache  删除") {
                scope.launch {
                    coroutinesCache.deleteItem("你好")
                }
            }

            CommonButton(text = "coroutinesCache lifecycle") {
                scope.launch {
                    coroutinesStr = getCoroutineCache(coroutinesCache, true)
                }
            }

            CommonButton(text = "coroutinesCache 删除 lifeCycle") {
                scope.launch {
                    coroutinesCache.deleteItem("你好", true)
                }
            }
        }
    }

    private data class WaHaHa(val name: String, val time: Long = System.currentTimeMillis())

    private suspend fun getCoroutineCache(
        cache: CoroutinesCache,
        lifeCycle: Boolean = false
    ): String {
        val waHaHa = cache.asyncCache(
            {
                WaHaHa("${if (lifeCycle) "lifeCycle." else ""}哇哈哈")
            },
            "你好",
            cachePolicy = if (lifeCycle)
                CachePolicy.LifecycleCache
            else
                CachePolicy.TimeCache(duration = 5, TimeUnit.MINUTES)
        )
        return "${waHaHa.name}，${waHaHa.time.yyyyMMddHHmmss}"
    }

    @Preview(showBackground = true, backgroundColor = 0xffffffff)
    @Composable
    fun RenderPreView() {

    }
}